<?php

namespace app\admin\model\advertiser;

use app\admin\model\base\User;
use app\admin\model\base\UserOfficial;
use app\admin\model\base\UserProgram;
use app\admin\model\iot\ScanBagWay;
use app\BaseModel;
use platform\DcPlatform;
use platform\LinSuPlatform;
use platform\LvBaoPlatform;
use platform\MaYouPlatform;
use platform\MeiPaPlatform;
use platform\YunDaiPlatform;
use think\facade\Log;

/**
 * 广告聚合平台管理
 */
class AdvertiserThird extends BaseModel
{

    /**
     * 第三方平台定义
     * @var array|array[]
     */
    public static array $ThirdPlatformArr = [
        ['id' => 1, 'type' => 'OfficialAccount', 'name' => '云袋公众号', 'prohibit' => [], 'remark' => '云袋公众号',],
        ['id' => 2, 'type' => 'Miniprogram', 'name' => '龙湖天街小程序-美葩', 'prohibit' => [], 'remark' => '龙湖天街小程序-美葩',],
        ['id' => 3, 'type' => 'Miniprogram', 'name' => '龙湖天街小程序-医院-美葩', 'prohibit' => [], 'remark' => '龙湖天街小程序-医院-美葩',],
        ['id' => 4, 'type' => 'OfficialAccount', 'name' => '绿葆公众号', 'prohibit' => [], 'remark' => '绿葆公众号',],
        ['id' => 5, 'type' => 'Miniprogram', 'name' => '龙湖天街小程序-重庆新沐光', 'prohibit' => [], 'remark' => '龙湖天街小程序-重庆新沐光',],
        ['id' => 6, 'type' => 'OfficialAccount', 'name' => '美葩公众号', 'prohibit' => [], 'remark' => '美葩公众号',],
        ['id' => 7, 'type' => 'Miniprogram', 'name' => '复星健康小程序', 'prohibit' => [], 'remark' => '复星健康小程序',],
        ['id' => 8, 'type' => 'Miniprogram', 'name' => '龙湖天街花龙珠-重庆新沐光', 'prohibit' => [], 'remark' => '龙湖天街花龙珠-重庆新沐光',],
    ];

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList(array $where, int $page, int $limit): array
    {
        $data = self::where($where)
            ->order('id desc')
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        //生成对应的模型
        $UserOfficialModel = new UserOfficial();
        $UserProgramModel = new UserProgram();
        //遍历数组
        foreach ($data['data'] as &$v) {
            //生成对应的模型
            $LogModel = $v['type'] == 'OfficialAccount' ? $UserOfficialModel : $UserProgramModel;
            //统计总次数(临时使用)
            $v['complete'] = $LogModel::where(['third_id' => $v['id']])->count();
            //解析禁止设备
            $v['prohibit'] = json_decode($v['prohibit'], true);
        }
        return $data;
    }

    /**
     * 查询开启的聚合平台
     * @param int $id
     * @return array
     */
    public static function getIDThird(int $id): array
    {
        //查询
        $where = ['id' => $id, 'enable' => 1];
        $data = self::where($where)->cache(true, 60)->find();
        if ($data) {
            $data = $data->toArray();
            $data['prohibit'] = json_decode($data['prohibit'], true);
            return $data;
        }
        return [];
    }

    /**
     * 获取聚合平台公众号信息
     * @param int $user_id
     * @param int $third_id
     * @param string $mac_id
     * @param string $thirdopenid
     * @param int $lsId
     * @return array
     */
    public static function getIdOfficialAccount(int $user_id, int $third_id, string $mac_id, string $thirdopenid = '',int $lsId = 0): array
    {
        //获取第三方平台信息
        $third = AdvertiserThird::getIDThird($third_id);
        //存在且过滤指定设备
        if (!$third or in_array($mac_id, $third['prohibit'])) {
            return [];
        }
        //获取用户信息
        $user = User::getIdUserInfo($user_id);
        if (!$user) {
            return [];
        }
        //获取对应的公众号
        switch ($third_id) {
            case 1:
                //云袋公众号
                try {
                    $YunFenBa = new YunDaiPlatform();
                    $yfb = $YunFenBa->queryInterface($user['uuid'], $mac_id, $mac_id, $user['nickname'], 0);
                    //判断云袋科技有没有公众号
                    if ($yfb['errcode'] == 0 and isset($yfb['data']['wechat_appid'])) {
                        $wechat_appid = $yfb['data']['wechat_appid'];
                        //判断当前用户是否关注过此公众号
                        $exist = UserOfficial::itExist($user_id, $wechat_appid);
                        if (!$exist) {
                            //写入日志
                            AdvertiserThirdLog::writeOfficialAccountLog($user_id, $third_id, $wechat_appid, $mac_id);
                            //当前流程参数
                            $temp['name'] = $yfb['data']['wechat_name'];
                            $temp['appid'] = $wechat_appid;
                            $temp['qrcode'] = $yfb['data']['qrcode_url'];
                            $temp['is_qrcode'] = true;
                            $temp['current_process'] = 'yd_official_account';
                            return $temp;
                        }
                    }
                } catch (\Exception $exception) {
                    Log::write($exception->getMessage(), '获取云袋公众号错误');
                }
                break;
            case 4:
                //绿葆公众号
                try {
                    $LvBao = new LvBaoPlatform();
                    $lb = $LvBao->queryInterface($user['uuid'], $user['nickname'], $user['sex'], $mac_id);
                    //判断当前用户是否关注过此公众号
                    if ($lb) {
                        $wechat_appid = $lb['wxAppId'];
                        $exist = UserOfficial::itExist($user_id, $wechat_appid);
                        if (!$exist) {
                            //写入日志
                            AdvertiserThirdLog::writeOfficialAccountLog($user_id, $third_id, $wechat_appid, $mac_id);
                            //当前流程参数
                            $temp['name'] = $lb['wxName'];
                            $temp['appid'] = $wechat_appid;
                            $temp['qrcode'] = $lb['qrcodeUrl'];
                            $temp['is_qrcode'] = true;
                            $temp['current_process'] = 'lb_official_account';
                            return $temp;
                        }
                    }
                } catch (\Exception $exception) {
                    Log::write($exception->getMessage(), '获取绿葆公众号错误');
                }
                break;
            case 6:
                //美葩公众号
                try {
                    $MeiPa = new MeiPaPlatform();
                    $mp = $MeiPa->queryInterface($user, $thirdopenid,$mac_id);
                    //判断当前用户是否关注过此公众号
                    if ($mp) {
                        $wechat_appid = $mp['wechat_appid'];
                        $exist = UserOfficial::itExist($user_id, $wechat_appid);
                        if (!$exist) {
                            //写入日志
                            AdvertiserThirdLog::writeOfficialAccountLog($user_id, $third_id, $wechat_appid, $mac_id);
                            //当前流程参数
                            $temp['name'] = $mp['wechat_name'];
                            $temp['appid'] = $wechat_appid;
                            $temp['qrcode'] = $mp['qrcodeurl'];
                            $temp['is_qrcode'] = true;
                            $temp['current_process'] = 'mp_official_account';
                            return $temp;
                        }
                    }
                } catch (\Exception $exception) {
                    Log::write($exception->getMessage(), '获取美葩公众号错误');
                }
                break;
            case 9:
                //零塑公众号
                try {
                    $Linsu = new LinSuPlatform();
                    $ls = $Linsu->queryInterface($user,$mac_id, $lsId);
                    //判断当前用户是否关注过此公众号
                    if ($ls) {
                        $wechat_appid = $ls['appId'];
                        $exist = UserOfficial::itExist($user_id, $wechat_appid);
                        if (!$exist) {
                            //写入日志
                            AdvertiserThirdLog::writeOfficialAccountLog($user_id, $third_id, $wechat_appid, $mac_id);
                            //当前流程参数
                            $temp['name'] = $ls['appName'];
                            $temp['appid'] = $wechat_appid;
                            $temp['qrcode'] = $ls['url'];
                            $temp['is_qrcode'] = true;
                            $temp['current_process'] = 'ls_official_account';
                            return $temp;
                        }
                    }
                } catch (\Exception $exception) {
                    Log::write($exception->getMessage(), '获取零塑公众号错误');
                }
                break;
            case 18:
                //东传科技
                try {
                    $Linsu = new DcPlatform();
                    $ls = $Linsu->queryInterface($user);
                    //判断当前用户是否关注过此公众号
                    if ($ls) {
                        $wechat_appid = $ls['appid'];
                        $exist = UserOfficial::itExist($user_id, $wechat_appid);
                        if (!$exist) {
                            //写入日志
                            AdvertiserThirdLog::writeOfficialAccountLog($user_id, $third_id, $wechat_appid, $mac_id);
                            //当前流程参数
                            $temp['name'] = $ls['appname'];
                            $temp['appid'] = $wechat_appid;
                            $temp['qrcode'] = $ls['qrcodeUrl'];
                            $temp['is_qrcode'] = true;
                            $temp['current_process'] = 'dc_official_account';
                            return $temp;
                        }
                    }
                } catch (\Exception $exception) {
                    Log::write($exception->getMessage(), '获取东传公众号错误');
                }
                break;
            case 19:
                //码友拓客
                $WayInfo = ScanBagWay::getMacIdWay($mac_id);
                $url = $WayInfo['jump_uri'];
                try {
                    $Linsu = new MaYouPlatform();
                    $ls = $Linsu->queryInterface($user,$mac_id,$url);
                    //判断当前用户是否关注过此公众号
                    if ($ls) {
                        $wechat_appid = $ls['wechatAppid'];
                        $exist = UserOfficial::itExist($user_id, $wechat_appid);
                        if (!$exist) {
                            //写入日志
                            AdvertiserThirdLog::writeOfficialAccountLog($user_id, $third_id, $wechat_appid, $mac_id);
                            //当前流程参数
                            $temp['name'] = $ls['wechatName'];
                            $temp['appid'] = $wechat_appid;
                            $temp['qrcode'] = $ls['qrcodeUrl'];
                            $temp['is_qrcode'] = true;
                            $temp['current_process'] = 'my_official_account';
                            return $temp;
                        }
                    }
                } catch (\Exception $exception) {
                    Log::write($exception->getMessage(), '获取码友公众号错误');
                }
                break;
            default:
                break;
        }
        return [];
    }

}